home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_08_03 / 8n03073a < prev    next >
Text File  |  1990-03-18  |  14KB  |  609 lines

  1. *****Listing 1*****
  2.  
  3.  
  4.        /*************************************************************
  5.        *
  6.        *       file d:\tc\cujds.c
  7.        *
  8.        *       Functions: This file contains
  9.        *          main
  10.        *          display_belief_vector
  11.        *          clear_belief_vector
  12.        *          enter_belief_vector
  13.        *          combine_using_dempsters_rule
  14.        *
  15.        *       Purpose:
  16.        *          This program demonstrates how to implement Dempster's
  17.        *          rule of combination.
  18.        *
  19.        *       NOTE: This is written for Borland's Turbo C
  20.        *             Version 1.5.  This allows us to use some
  21.        *             nice user interface functions.  The actual
  22.        *             combination code is compiler independent.
  23.        *
  24.        *************************************************************/
  25.  
  26.  
  27. extern unsigned int _stklen = 40000;
  28.  
  29.  
  30. #include "d:\tc\include\stdio.h"
  31. #include "d:\tc\include\io.h"
  32. #include "d:\tc\include\fcntl.h"
  33. #include "d:\tc\include\dos.h"
  34. #include "d:\tc\include\math.h"
  35. #include "d:\tc\include\graphics.h"
  36. #include "d:\tc\include\conio.h"
  37. #include "d:\tc\include\sys\stat.h"
  38.  
  39. #define LENGTH_OF_BELIEF_VECTOR 8
  40.  
  41.  
  42.  
  43.  
  44. main()
  45. {
  46.  
  47.    char   response[80];
  48.  
  49.    int    choice,
  50.           i,
  51.           j,
  52.           not_finished;
  53.  
  54.    short  place;
  55.  
  56.  
  57.    float a[LENGTH_OF_BELIEF_VECTOR],
  58.          belief,
  59.          v[LENGTH_OF_BELIEF_VECTOR];
  60.  
  61.  
  62.  
  63.    textbackground(1);
  64.    textcolor(7);
  65.    clrscr();
  66.  
  67.    not_finished = 1;
  68.    while(not_finished){
  69.  
  70.       clrscr();
  71.       printf("\n> You may now either:");
  72.       printf("\n            1. Start the process");
  73.       printf("\n            2. Enter more assertions");
  74.       printf("\n            3. Exit program");
  75.       printf("\n        _\b");
  76.       get_integer(&choice);
  77.  
  78.       switch (choice){
  79.  
  80.          case 1:
  81.             clear_belief_vector(v);
  82.             clear_belief_vector(a);
  83.             clrscr();
  84.             enter_belief_vector(v, 1);
  85.             clrscr();
  86.             enter_belief_vector(a, 1);
  87.             clrscr();
  88.             printf("\n> Initial Belief Vector\n");
  89.             display_belief_vector(v);
  90.             printf("\n> Second Belief Vector\n");
  91.             display_belief_vector(a);
  92.             combine_using_dempsters_rule(v, a);
  93.             printf("\n> Resultant Belief Vector\n");
  94.             display_belief_vector(v);
  95.             break;
  96.  
  97.          case 2:
  98.             clrscr();
  99.             clear_belief_vector(a);
  100.             enter_belief_vector(a, 1);
  101.             clrscr();
  102.             printf("\n> Initial Belief Vector\n");
  103.             display_belief_vector(v);
  104.             printf("\n> Second Belief Vector\n");
  105.             display_belief_vector(a);
  106.             combine_using_dempsters_rule(v, a);
  107.             printf("\n> Resultant Belief Vector\n");
  108.             display_belief_vector(v);
  109.             break;
  110.  
  111.          case 3:
  112.             not_finished = 0;
  113.             break;
  114.  
  115.       }  /* ends switch choice  */
  116.    }     /* ends while not_finished  */
  117. }        /* ends main */
  118.  
  119.  
  120.  
  121. clear_belief_vector(v)
  122.    float  v[];
  123. {
  124.    int i;
  125.  
  126.    for(i=0; i<LENGTH_OF_BELIEF_VECTOR; i++)
  127.       v[i] = 0.0;
  128. }  /*  ends clear_belief_vector  */
  129.  
  130.  
  131.  
  132.  
  133. display_belief_vector(v)
  134.    float  v[];
  135. {
  136.    int i, j;
  137.    char response[80];
  138.  
  139.    j=1;
  140.    for(i=0; i<LENGTH_OF_BELIEF_VECTOR; i++){
  141.       if((j%5) == 0){
  142.          printf("\n");
  143.          j++;
  144.       }
  145.       if(v[i] > 0.0001){
  146.          printf("  [%3d]=%6f", i, v[i]);
  147.          j++;
  148.       }
  149.    }
  150.    printf("\n   Hit RETURN to continue");
  151.    read_string(response);
  152. }  /*  ends display_belief_vector  */
  153.  
  154.  
  155.  
  156.  
  157. enter_belief_vector(v, line)
  158.    float v[];
  159.    int   line;
  160. {
  161.    int   i,
  162.          not_finished,
  163.          y;
  164.    float value;
  165.    y = line;
  166.  
  167.    printf("\n> ENTER BELIEF VECTOR");
  168.  
  169.    printf("\n> Enter the place (RETURN) and value (RETURN)");
  170.    printf("\n> (Enter -1 for place when you're finished)");
  171.  
  172.    not_finished = 1;
  173.    while(not_finished){
  174.       printf("\n   [___]=______");
  175.       y = wherey();
  176.       gotoxy(5, y);
  177.       get_integer(&i);
  178.       gotoxy(10, y);
  179.       get_float(&value);
  180.  
  181.       if(i != -1){
  182.          v[i] = value;
  183.       }  /* ends if i 1+ -1  */
  184.       else
  185.          not_finished = 0;
  186.  
  187.    }  /* ends while not_finished  */
  188. }     /* ends enter_belief_vector  */
  189.  
  190.  
  191.  
  192.  
  193.        /************************************************************
  194.        *
  195.        *   This is the function that implements Demptser's rule
  196.        *   of combination.
  197.        *   vector1 holds the original beliefs and will hold the
  198.        *   result of the combination.
  199.        *
  200.        ************************************************************/
  201.  
  202. combine_using_dempsters_rule(vector1, vector2)
  203.    float vector1[LENGTH_OF_BELIEF_VECTOR],
  204.          vector2[LENGTH_OF_BELIEF_VECTOR];
  205. {
  206. float denominator,
  207.       sum_vector[LENGTH_OF_BELIEF_VECTOR];
  208.  
  209. int   a,
  210.       i,
  211.       place;
  212.  
  213. /* set the sums to zero */
  214. for(i=0; i<LENGTH_OF_BELIEF_VECTOR; i++)
  215.    sum_vector[i] = 0.0;
  216.  
  217. /* Now go through the intersection tableau.      */
  218. /* Look for the intersection of non-zero beliefs */
  219. /* and save their products.                      */
  220.  
  221. for(a=1; a<LENGTH_OF_BELIEF_VECTOR; a++){
  222.    if(vector2[a] > 0.0){
  223.       for(i=0; i<LENGTH_OF_BELIEF_VECTOR; i++){
  224.          place = i & a;
  225.          if(vector1[i] > 0.0)
  226.           sum_vector[place] = (vector1[i] * vector2[a]) + sum_vector[place];
  227.       }  /* ends loop over i */
  228.    }  /* ends if vector2[a] > 0.0 */
  229. }  /* ends loop over a */
  230.  
  231. denominator = 1.0 - sum_vector[0];
  232. for(i=1; i<LENGTH_OF_BELIEF_VECTOR; i++)
  233.    vector1[i] = sum_vector[i]/denominator;
  234.  
  235.  
  236. }  /* ends combine_using_dempsters_rule */
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.    /* The following functions are I-O  */
  244.  
  245.  
  246.  
  247. read_string(string)
  248.         char *string;
  249. {
  250.         int     eof,
  251.                 letter,
  252.                 no_error;
  253.  
  254.         eof = -1;
  255.         no_error = 0;
  256.  
  257.         while((letter = getchar()) != '\n' &&
  258.                letter !=  eof)
  259.            *string++ = letter;
  260.  
  261.         *string = '\0';
  262.  
  263.         return((letter == eof) ? eof : no_error);
  264.  
  265. }       /* ends read_string */
  266.  
  267.  
  268.  
  269. clear_buffer(string)
  270.    char string[];
  271. {
  272.    int i;
  273.    for(i=0; i<80; i++)
  274.       string[i] = ' ';
  275. }
  276.  
  277.  
  278.  
  279. long_clear_buffer(string)
  280.    char string[];
  281. {
  282.    int i;
  283.    for(i=0; i<300; i++)
  284.       string[i] = ' ';
  285. }
  286.  
  287.  
  288.  
  289.  
  290. #define is_digit(x) ((x >= '0' && x <= '9') ? 1 : 0)
  291.  
  292. #define is_blank(x) ((x == ' ') ? 1 : 0)
  293.  
  294. #define to_decimal(x) (x - '0')
  295.  
  296. #define NO_ERROR  0
  297. #define IO_ERROR -1
  298. #define NULL2   '\0'
  299.  
  300.  
  301. get_integer(n)
  302.    int *n;
  303. {
  304.    char string[80];
  305.  
  306.    read_string(string);
  307.    int_convert(string, n);
  308. }
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  int_convert (ascii_val, result)
  315.     char *ascii_val;
  316.     int *result;
  317.   {
  318.     int sign = 1;  /* -1 if negative */
  319.  
  320.     *result = 0;   /* value returned to the calling routine */
  321.  
  322.     /* read passed blanks */
  323.  
  324.     while (is_blank(*ascii_val))
  325.        ascii_val++;              /* get next letter */
  326.  
  327.     /* check for sign */
  328.  
  329.     if (*ascii_val == '-' || *ascii_val == '+')
  330.        sign = (*ascii_val++ == '-') ? -1 : 1;  /* find sign */
  331.  
  332.    /*
  333.     * convert the ASCII representation to the actual
  334.     * decimal value by subtracting '0' from each character.
  335.     *
  336.     * for example, the ASCII '9' is equivalent to 57 in decimal.
  337.     * by subtracting '0' (or 48 in decimal) we get the desired
  338.     * value.
  339.     *
  340.     * if we have already converted '9' to 9 and the next character
  341.     * is '3', we must first multiply 9 by 10 and then convert '3'
  342.     * to decimal and add it to the previous total yielding 93.
  343.     *
  344.     */
  345.  
  346.     while (*ascii_val)
  347.      if (is_digit(*ascii_val))
  348.        *result = *result * 10 + to_decimal(*ascii_val++);
  349.  
  350.      else
  351.        return (IO_ERROR);
  352.  
  353.  
  354.     *result = *result * sign;
  355.  
  356.     return (NO_ERROR);
  357.   }
  358.  
  359.  
  360.  
  361.  
  362.  
  363. get_short(n)
  364.    short *n;
  365. {
  366.    char string[80];
  367.  
  368.    read_string(string);
  369.    int_convert(string, n);
  370. }
  371.  
  372.  
  373.  
  374.  
  375.  
  376.  short_convert (ascii_val, result)
  377.     char *a